unity stop physics

71

unity stop physics -

//if you have multiple scenes running concurrently and want to stop some of them
bool isRunning = false;
Scene _currScene;
PhysicsScene _physicsScene;

void start()
{
  //first disable auto simualtion and control 
  //control when do you want the simulation to 
  //run by a boolean
   Physics.autoSimulation = false;
   isRunning = true;
  //get the physics scene that you want it to be running 
  _currScene = SceneManager.GetActiveScene();
  _physicsScene = _currScene.GetPhysicsScene();
  // a boolean to stop the physcis in a scene
 
}
void FixedUpdate()
{
  if (_currScene.IsValid() && isRunning )
    {
      _physicsScene.Simulate(Time.deltaTime);
    }
}
void disablePhysics(){ isRunning = false;}
void enablePhysics(){ isRunning = true;}

// another way to stop the physics is by changing the time scale from 1 to 0 
void disablePhysics(){ Time.timeScale = 0;}
void enablePhysics(){ Time.timeScale = 1;}
// remember that anything that moves in the scene using methods that you coded
//must be linked to Time.deltaTime so you don't end up with some objects out of sync
//check the source for more info

Comments

Submit
0 Comments